package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.openfeign.VodClient;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ZhanK
 * @since 2022-02-20
 */
@Api(description = "添加课程管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    EduCourseService eduCourseService;
    @Autowired
    EduVideoService eduVideoService;
    //注入远程调用openfeign的接口依赖
    @Autowired
    VodClient vodClient;


    @ApiOperation("添加课程及简介到数据库")
    @PostMapping(value = ("/addCourseInfo"))
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后的课程id，为了后面添加大纲使用
        String cid = eduCourseService.saveCourseInfoVo(courseInfoVo);
        return R.ok().data("courseId", cid);
    }

    //根据课程id查询课程的基本信息
    @GetMapping(value = ("getCourseInfo/{courseId}"))
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation("//根据课程id查询课程确认信息")
    @GetMapping(value = ("getPublishCourseInfo/{id}"))
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse", publishCourseInfo);

    }

    @ApiOperation("根据课程id发布课程")
    @GetMapping(value = ("publishCourse/{id}"))
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();

    }

    @ApiOperation("课程根据条件分页查询")
    @PostMapping(value = ("/pageCourseCondition/{current}/{limit}"))
    //Required request body is missing: 请求正文丢失是因为@PostMapping 写成了@GetMapping
    public R pageCourseCondition(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody CoursePublishVo coursePublishVo) {

        Page<CoursePublishVo> page = new Page<>(current, limit);
//       List<CoursePublishVo>  list = eduCourseService.getPageCourse();
        Page<CoursePublishVo> pageCourse = eduCourseService.getPageCourse(page, coursePublishVo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", pageCourse.getTotal());//获取总记录数
        map.put("rows", pageCourse.getRecords());//获取分页后的list集合

        return R.ok().data(map);

    }

    @ApiOperation("根据课程ID删除课程及章节、小节")
    @GetMapping(value = ("/deleteCourseById/{id}"))
    public R deleteCourseById(@PathVariable String id) {
        //先根据课程ID查出所关联的视频，然后把这些视频的阿里云ID整合到一个String的集合里面给vod模块调用阿里云sdk批量删除。
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        wrapper.select("video_source_id");
        List<EduVideo> list = eduVideoService.list(wrapper);
        ArrayList<String> videos = new ArrayList<>();
        if (list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                videos.add(list.get(i).getVideoSourceId());
            }
        }
        if (videos.size()>0){
            //调用远程接口，测试熔断器效果
            R r = vodClient.removeVideoList(videos);
            if(r.getCode()== 20001){ //这个是调用了消费者的服务降级，因为没收到vod服务器的响应
                throw new GuliException(20001,r.getMessage());
            }
            if (r.getCode()==20002){ //这个是收到了vod服务自己降级后的返回值，vod自身服务降级后传给edu

                throw new GuliException(20001,r.getMessage());
            }
        }

        int i = eduCourseService.deleteCourseById(id);
        return R.ok().data("delete", i);
    }

}

